package xcmg.device.web.threeGuaranteesPur;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.threeGuaranteesPur.ThreeReverseService;
import xcmg.device.service.vo.threeGuaranteesPur.ThreeReverseAddVO;
import xcmg.device.service.vo.threeGuaranteesPur.ThreeReverseVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

@RestController
@RequestMapping("/threeReverse")
public class ThreeReverseController extends BasicController {
    @Autowired
    private ThreeReverseService threeReverseService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "增加冲销入库记录")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody ThreeReverseAddVO threeReverseAddVO)throws Exception{
        if(null == threeReverseAddVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode;
        try {
            errorCode = threeReverseService.add(threeReverseAddVO, getUserId(), getOrgId());
            return actionResult(errorCode);
        }catch (Exception exc){
            return new ActionResult(ErrorCode.Failure.getCode(), exc.getMessage());
        }
    }

    @RequestMapping(value = "/audit",method = RequestMethod.POST)
    @ApiOperation(value = "审核三包冲销单")
    @FastMappingInfo(needLogin = true)
    public ActionResult audit(@RequestBody ThreeReverseVO threeReverseVO)throws Exception{
        if(null == threeReverseVO){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode errorCode;
        try {
            errorCode = threeReverseService.audit(threeReverseVO, getOrgId(), getUserId());
            return actionResult(errorCode);
        }catch (Exception exc){
            return new ActionResult(ErrorCode.Failure.getCode(), exc.getMessage());
        }
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @ApiOperation("冲销入库记录删除")
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids)throws Exception{
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            return actionResult(threeReverseService.delete(Arrays.asList(ids), getOrgId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id)throws Exception{
        return actionValueResult(threeReverseService.findByPK(id));
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation("查询冲销入库记录列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<ThreeReverseVO> condition)throws Exception{
        ThreeReverseVO threeReverseVO =condition.getFilters();
        if(threeReverseVO == null){
            threeReverseVO = new ThreeReverseVO();
        }
        threeReverseVO.setOrgId(getOrgId());
        condition.setFilters(threeReverseVO);
        PageCommonVO pageCommonVO= threeReverseService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @ApiOperation("导出冲销入库记录列表")
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        threeReverseService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @ApiOperation("导出全部冲销入库记录列表")
    @FastMappingInfo(needLogin = true)
    public void exportAll(ThreeReverseVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        threeReverseService.exportAll(condition);
    }

//    @RequestMapping(value = "/sapPost", method = RequestMethod.POST)
//    @ApiOperation(value = "三包冲销过账")
//    @FastMappingInfo(needLogin = true)
//    public ActionResult sapRemove(@RequestBody ThreeReverseDO threeReverseDO) {
//        if(threeReverseDO == null){
//            return actionResult(ErrorCode.IllegalArument);
//        }
//        ErrorCode errorCode;
//        try {
//            errorCode = threeReverseService.sapPost(threeReverseDO.getId(), getOrgId());
//        } catch (Exception e) {
//            LogHelper.monitor(e.getMessage());
//            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
//        }
//        return actionResult(errorCode);
//    }
}
